import { Color } from 'https://unpkg.com/three/build/three.module.js';
import { initShaders } from '../jsm/Utils.js';
import lib from '../jsm/ThreeLib.js';
import Poly from '../jsm/Poly.js'
import Sky from '../jsm/Sky.js'

window.onload = function () {

    const backColor = new Color("rgba(0,0,0,1)");

    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const gl = canvas.getContext("webgl");
    //开启颜色合成,才会有透明度的感觉
    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);

    //初始化着色器
    initShaders(gl,
        document.querySelector("#vertexShader").innerText,
        document.querySelector("#fragmentShader").innerText);
    lib.clearCanvas(gl, new Color("rgba(0,0,0,1)"), 1);

    canvas.oncontextmenu = function () {
        return false;
    }
    const sky = new Sky(gl);
    let poly = null;




    canvas.addEventListener("mousedown", function (event) {
        lib.clearCanvas(gl, new Color("rgba(0,0,0,1)"), 1);
        if (event.button === 2) {
            //停止绘制
            if (!!poly) {
                sky.add(poly);
                poly = null;
            }
        } else {
            if (!poly) {
                poly = new Poly({ gl: gl });
            }

            var { x, y } = lib.GetCanvasPosi(event, canvas);

            poly.addVertice(x, y);
            poly.draw(["POINTS", "LINE_STRIP"]);
        }
        sky.draw(["POINTS", "LINE_STRIP"]);

    })




}